<hr/>
<div class="row-fluid">
    <div class="span12">
        <button class="btn btn-success" id="btn_save">保存</button>
        <a href="${request.contextPath}/stock/list" class="btn btn-info" id="btn_back">返回列表</a>
        <div class="widget-box">
            <div class="widget-title"> <span class="icon"> <i class="icon-pencil"></i> </span>
                <h5>出入库信息</h5>
            </div>
            <div class="widget-content nopadding">
                <form class="form-horizontal" method="post" action="${request.contextPath}/stock/saveStock" name="stock_form" id="stock_form">
                    <input type="hidden" name="stockDetailId" value="${detail.stockDetailId}" />
                    <div class="control-group">
                        <label class="control-label">商品：</label>
                        <div class="controls">
                            <select name="productId">
                                <#list productList as product>
                                    <option value="${product.productId}" <#if product.productId == detail.product.productId >selected</#if>>[${product.productCode}]${product.productName}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">商品规格：</label>
                        <div class="controls">
                            <select name="productSkuId">

                            </select>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">出入库数量:</label>
                        <div class="controls">
                            <input type="text" name="amount" value="${detail.amount}" />
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">采购价:</label>
                        <div class="controls">
                            <input type="text" name="purchasePrice" value="${detail.purchasePrice}" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="${request.contextPath}/js/jquery-1.12.2.min.js"></script>
<script src="${request.contextPath}/js/jquery.validate.js"></script>
<script src="${request.contextPath}/js/sweetalert.min.js"></script>
<script src="${request.contextPath}/js/select2.full.min.js"></script>
<script type="text/javascript">

    var stock_product_id = "${detail.product.productId}";
    var stock_productSku_id = "${detail.productSKU.productSkuId}";

    $(function(){
        $("select").select2();

        stock_product_id = stock_product_id.length > 0 ? stock_product_id : $("select[name='productId']").val();
        getProductSkuList(stock_product_id);

        $("select[name='productId']").change(function(){
            var productId = $(this).val();
//            console.log("productId:" + productId);
            getProductSkuList(productId);
        });

        $("#btn_back").click(function(){
            loadPage($(this).attr("href"));
            return false;
        });

        $("#btn_save").click(function(){
            $("#stock_form").submit();
        });


        $("#stock_form").validate({
            submitHandler: function(form){
                $.ajax({
                    url: $(form).attr("action"),
                    type: "post",
                    data: $(form).serialize(),
                    success: function(result){
                        swal({
                            title: "信息提示",
                            text: result.status == "success" ? "恭喜，操作已完成！" : "很遗憾，操作失败了！",
                            type: result.status,
                            animation: "slide-from-top"
                        }, function(){
                            if (result.status == "success"){
                                loadPage("${request.contextPath}/stock/list");
                            }
                        });
                    }
                });
            },
            rules: {
                specName: {
                    "required": true
                }
            },
            errorClass: "help-inline",
            errorElement: "span",
            highlight:function(element, errorClass, validClass) {
                $(element).parents('.control-group').addClass('error');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).parents('.control-group').removeClass('error');
                $(element).parents('.control-group').addClass('success');
            }

        });
    });

    function getProductSkuList(productId){
        $.post("${request.contextPath}/product/getSkuListByProductId", {productId: productId}, function(result){
//            console.log(result.status);
            if (result.status == "success"){
                var sku_select = $("select[name='productSkuId']");
                sku_select.html("");
                $.each(result.data, function(i, n){
//                    console.log(n.skuName);
                    if (n.productSkuId == stock_productSku_id){
                        sku_select.append("<option value='" + n.productSkuId + "' selected>[" + n.skuCode + "]"+ n.skuName + "</option>");
                    }
                    else{
                        sku_select.append("<option value='" + n.productSkuId + "'>[" + n.skuCode + "]"+ n.skuName + "</option>");
                    }
                });
                sku_select.select2();
            }
        });
    }
</script>